package com.javaclimb.music.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.javaclimb.music.domain.Comment;
import com.javaclimb.music.exection.BizCodeEnum;
import com.javaclimb.music.service.CommentService;
import com.javaclimb.music.utils.PageUtils;
import com.javaclimb.music.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 评论控制类
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 添加修改评论
     */
    @PostMapping(value = "/saveOrUpdate")
    public R addComment(@RequestBody Comment comment) {
        boolean flag = commentService.saveOrUpdate(comment);
        if (flag) {   //保存成功
            return R.ok().put("data", comment);
        }
        return R.error(BizCodeEnum.ADD_OR_UPDATE_ERROR);
    }


    /**
     * 删除评论
     */
    @PostMapping(value = "/delete")
    public R deleteComment(@RequestBody Integer[] ids) {

        boolean flag = commentService.removeByIds(Arrays.asList(ids));
        if (flag) {
            return R.ok();
        }
        return R.error(BizCodeEnum.DELETE_ERROR);
    }

    /**
     * 根据主键查询整个对象
     */
    @GetMapping("/info/{id}")
    public Object selectByPrimaryKey(@PathVariable("id") Integer id) {
        return commentService.getById(id);
    }

    /**
     * 查询所有评论
     */
    @GetMapping("/allComment")
    public Object allComment(@RequestParam Map<String, Object> params) {
        PageUtils pageUtils = commentService.queryPage(params);
        return R.ok().put("data", pageUtils);
    }

    /**
     * 查询某个歌曲下的所有评论
     */
    @GetMapping("/commentOfSongId")
    public Object commentOfSongId(Integer songId) {
        List<Comment> list = commentService.list(new LambdaQueryWrapper<Comment>().eq(Comment::getSongId, songId));
        return R.ok().put("data", list);
    }

    /**
     * 查询某个歌单下的所有评论
     */
    @GetMapping(value = "/commentOfSongListId")
    public R commentOfSongListId(Integer songListId) {
        List<Comment> list = commentService.list(new LambdaQueryWrapper<Comment>().eq(Comment::getSongListId, songListId));
        return R.ok().put("data", list);
    }

    /**
     * 给某个评论点赞
     */
    @PostMapping("/like")
    public Object like(@RequestBody Comment comment) {

        //只修改点赞数
        Comment newComment = new Comment();
        newComment.setId(comment.getId());
        newComment.setUp(comment.getUp());
        //保存到评论的对象中
        boolean flag = commentService.updateById(newComment);
        if (flag) {   //保存成功
            return R.ok();
        }
        return R.error(BizCodeEnum.UPDATE_ERROR);
    }

}






















